<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of admins
 *
 * @author Maulnick
 */
require_once 'document_entity.php';
class Freightinvoice_Entity extends Document_Entity{
    public function load($id){
        // retrieve from database
        $result = array();
        $result['data'] = $this->get_freightinvoice($id);
        $lineitems = $this->get_freightinvoice_lineitem($result['data']->freightinvoice_id);
        $container_details = array();
        foreach($lineitems as $lineitem){
            $container_detail = array();
            $container_detail = $lineitem;
            $container_detail->ContainerDetail = $this->get_freightinvoice_containerdetail($lineitem->freightinvoice_lineitem_id);
            array_push($container_details, $container_detail);
        }
        $result['lineitem'] = $container_details;
        $this->set_data($result);
        return $this->get_data();
    }
    public function save(){
        // insert to database
        $data = $this->get_data();
        $freightinvoice = $data['data'];
        // 1. insert freightinvoice
        $step_1 = $this->db->insert('freightinvoice',$freightinvoice);
        if($step_1){
            $this->document_id = $this->get_last_id();
            foreach($data['lineitem'] as $container){
                // 2. insert freightinvoice_lineitem
                $container['freightinvoice_id'] = $this->document_id;
                $container_details = $container['ContainerDetail'];
                unset($container['ContainerDetail']);
                $step_2 = $this->db->insert('freightinvoice_lineitem', $container); 
                if($step_2){
                    $id_container = $this->get_last_id_container();
                    foreach($container_details as $detail){
                        // 3. insert freightinvoice_lineitem_containerdetail
                        $detail['freightinvoice_lineitem_id'] = $id_container;
                        $step_3 = $this->db->insert('freightinvoice_lineitem_containerdetail', $detail);
                    }
                }
            }
        }
        return $step_1 && $step_2 && $step_3;
    }
    
    private function get_last_id(){
        //get last index freightinvoice
        $this->db->select('freightinvoice_id');
        $this->db->order_by('freightinvoice_id', 'DESC');
        $this->db->limit(1);
        $result = $this->db->get('freightinvoice');
        if($result->num_rows==1){
            return $result->row(0)->freightinvoice_id;
        }else{
            return false;
        }
    }
    private function get_last_id_container(){
        //get last index freightinvoice
        $this->db->select('freightinvoice_lineitem_id');
        $this->db->order_by('freightinvoice_lineitem_id', 'DESC');
        $this->db->limit(1);
        $result = $this->db->get('freightinvoice_lineitem');
        if($result->num_rows==1){
            return $result->row(0)->freightinvoice_lineitem_id;
        }else{
            return false;
        }
    }
    private function get_freightinvoice($freightinvoice_id){
        // get freightinvoice
        $this->db->where('freightinvoice_id',$freightinvoice_id);
        $result = $this->db->get('freightinvoice');
        if($result->num_rows==1){
            return $result->row(0);
        }else{
            return false;
        }
    }
    private function get_freightinvoice_lineitem($freightinvoice_id){
        // get freightinvoice_lineitem
        $this->db->where('freightinvoice_id',$freightinvoice_id);
        $result = $this->db->get('freightinvoice_lineitem');
        if($result->num_rows>0){
            return $result->result();
        }else{
            return false;
        }
    }
    private function get_freightinvoice_containerdetail($freightinvoice_lineitem_id){
        // get freightinvoice_lineitem_containerdetail
        $this->db->where('freightinvoice_lineitem_id',$freightinvoice_lineitem_id);
        $result = $this->db->get('freightinvoice_lineitem_containerdetail');
        if($result->num_rows>0){
            return $result->result();
        }else{
            return false;
        }
    }
    
    public function get_freightinvoice_by_owner($role_id, $user_id){
        // get array of freighvinvoice by its owner
        $freightinvoices = array();
        $this->db->where('role_user_id',$role_id);
        $this->db->where('user_id',$user_id);
        $this->db->where('message_placeholder_id',MESSAGE_INBOX);
        $result = $this->db->get('user_message_map');
        //print_r($result->result());
        if($result->num_rows>0){
            # get user_message_maps
            $user_message_maps = $result->result();
            foreach($user_message_maps as $user_message_map){
                $message_id = $user_message_map->message_id;
                $this->db->where('message_id',$message_id);
                $this->db->where('role_document_id',DOCUMENT_FREIGHTINVOICE);
                $result = $this->db->get('message_document');
                # get message_document
                if($result->num_rows>0){
                    $message_documents = $result->result();
                    foreach($message_documents as $message_document){
                        # get freightinvoice
                        $freightinvoice_id = $message_document->document_id;
                        $freightinvoice = $this->get_freightinvoice($freightinvoice_id);
                        if($freightinvoice){
                            if($freightinvoice->PaymentStatus!=ACTION_APPROVE){
                                array_push($freightinvoices, $freightinvoice);
                            }
                        }
                    }
                }
            }
            return $freightinvoices;
        }else{
            return false;
        }
    }
    
    public function update_approvement($freightinvoice_id,$approvement){
        $data = array(
               'PaymentStatus' => $approvement
            );
        $this->db->where('freightinvoice_id', $freightinvoice_id);
        return $this->db->update('freightinvoice', $data); 
    }
}